﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using orienta.Models;
using orienta.Helper;

namespace orienta.Controllers
{ 
    public class CompendioController : Controller
    {
        private GoodTireEntities db = new GoodTireEntities();

        //
        // GET: /Compendio/

        public ViewResult Index()
        {
            var orimaecompendio = db.ORIMaeCompendio.Include("ORIMaeUsuario");
            return View(orimaecompendio.ToList());
        }

        public ViewResult Seleccionar()
        {
            var orimaecompendio = db.ORIMaeCompendio.Include("ORIMaeUsuario");
            ViewBag.tituloPopup = Request["tituloCompendio"];
            int idCompendio = int.Parse( Request["idCompendio"]);
            CatalogoHelper helper = new CatalogoHelper();

            return View(helper.getCatalogoDetalleList(idCompendio));
        }


        public ViewResult ListaDetalle(decimal id)
        {
            var orimaecompendio = db.ORIMaeCompendio.Include("ORIMaeUsuario");
            ViewBag.tituloPopup = Request["tituloCompendio"];
            int idCompendio = int.Parse(Request["idCompendio"]);
            CatalogoHelper helper = new CatalogoHelper();

            return View(helper.getCatalogoDetalleList(idCompendio));
        }

        //
        // GET: /Compendio/Details/5

        public ViewResult Details(decimal id)
        {
            ORIMaeCompendio orimaecompendio = db.ORIMaeCompendio.Single(o => o.nIdeCompendio == id);
            return View(orimaecompendio);
        }

        //
        // GET: /Compendio/Create

        public ActionResult Create()
        {
            ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdUsu", "vCodUsuario");
            return View();
        } 

        //
        // POST: /Compendio/Create

        [HttpPost]
        public ActionResult Create(ORIMaeCompendio orimaecompendio)
        {
            if (ModelState.IsValid)
            {
                db.ORIMaeCompendio.AddObject(orimaecompendio);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdUsu", "vCodUsuario", orimaecompendio.nIdeUsuReg);
            return View(orimaecompendio);
        }
        
        //
        // GET: /Compendio/Edit/5
 
        public ActionResult Edit(decimal id)
        {
            ORIMaeCompendio orimaecompendio = db.ORIMaeCompendio.Single(o => o.nIdeCompendio == id);
            ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdUsu", "vCodUsuario", orimaecompendio.nIdeUsuReg);
            return View(orimaecompendio);
        }

        //
        // POST: /Compendio/Edit/5

        [HttpPost]
        public ActionResult Edit(ORIMaeCompendio orimaecompendio)
        {
            Response.Write("hola");
            System.Diagnostics.Debug.WriteLine("hola2");

            if (ModelState.IsValid)
            {
                db.ORIMaeCompendio.Attach(orimaecompendio);
                db.ObjectStateManager.ChangeObjectState(orimaecompendio, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.nIdUsu = new SelectList(db.ORIMaeUsuario, "nIdUsu", "vCodUsuario", orimaecompendio.nIdeUsuReg);
            return View(orimaecompendio);
        }

        //
        // GET: /Compendio/Delete/5
 
        public ActionResult Delete(decimal id)
        {
            ORIMaeCompendio orimaecompendio = db.ORIMaeCompendio.Single(o => o.nIdeCompendio == id);
            return View(orimaecompendio);
        }

        //
        // POST: /Compendio/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {            
            ORIMaeCompendio orimaecompendio = db.ORIMaeCompendio.Single(o => o.nIdeCompendio == id);
            db.ORIMaeCompendio.DeleteObject(orimaecompendio);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}